package com.yulongtian.weekCompetition.month202211.week4;

/**
 * 第 321 场周赛
 *
 * @author yulongTian
 * @create 2022-11-27 10:28
 */
public class Test01 {
    public static void main(String[] args) {
        System.out.println(pivotInteger(8));
    }


    public static int pivotInteger(int n) {
        int res = 0;
        if (n == 1) {
            return n;
        }
        if(n>1&&n<=4){
            return -1;
        }

        //1~n的总和
        int nSum = (1 + n) * n / 2;

        //求出答案附近的数
        int xNum = (int) Math.sqrt(nSum) - 1;

        //是否找到
        boolean flag = false;
        //数10个
        for (int i = xNum-1; i < n; i++) {
            if(i<=0){
                i=1;
            }
            if (i - xNum > 5) {
                break;
            }

            if(2*i*i==n+n*n){
                flag=true;
                res=i;
                break;
            }




        }
        if (flag) {
            return res;
        } else {
            return -1;
        }
    }


}
